﻿

Imports System.ComponentModel
Imports HslControls

Public Class frmLHJList

    Private objHWSetting As New clsHWSetting

    Private _selectedLHJ As String

    Private _selectedClose As Boolean = False

    Public Property SelectedLHJ As String
        Get
            Return _selectedLHJ
        End Get
        Set(value As String)
            _selectedLHJ = value
        End Set
    End Property

    Public Property SelectedClose As Boolean
        Get
            Return _selectedClose
        End Get
        Set(value As Boolean)
            _selectedClose = value
        End Set
    End Property

    Private Sub frmLHJList_Load(sender As Object, e As EventArgs) Handles Me.Load
        connectDB()
        'TLP.Controls.Add(New ucLHJ(ucLHJ.LHJ_STATUS.BUSY, 1, 1))
        'TLP.Controls.Add(New ucLHJ(ucLHJ.LHJ_STATUS.EMPTY, 2, 1))
        'TLP.Controls.Add(New ucLHJ(ucLHJ.LHJ_STATUS.LOSE_CONNECT, 3, 1))
        setTLP()
        lhjLoad()
        'pnlAlarm.Controls.Add(New usrLHJErr("dddddd", "HH-1-2-1-1"))
        'pnlAlarm.Controls(0).Dock = DockStyle.Fill
        'tmrRe.Start()

    End Sub
    Public Sub connectDB()
        'イニシャルファイル情報取得
        g_szInitFileName = My.Application.Info.DirectoryPath & "\LHJServer.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        '得到当前用户名
        g_LoginUser = GetIniFile("LOGIN", "user", "admin", g_szInitFileName)

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

        setAllErrMsg()
    End Sub

    Private Sub frmLHJList_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        DBacs.Disconnect()
    End Sub
    Private Sub setTLP()
        Dim maxRow As Integer = 0
        Dim maxCol As Integer = 0
        objHWSetting.getMaxRowCol(maxRow, maxCol)
        If maxRow > 0 And maxCol > 0 Then
            Me.TLP.RowStyles.Clear()
            Me.TLP.ColumnStyles.Clear()
            TLP.RowCount = maxRow
            TLP.ColumnCount = maxCol
            For i As Integer = 0 To maxRow
                Me.TLP.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100 / maxRow))
            Next
            For i As Integer = 0 To maxCol
                Me.TLP.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100 / maxCol))
            Next
        End If
    End Sub
    Private Sub lhjLoad()
        Dim dtLHJ As DataTable
        'Dim dtLHJDetail As DataTable
        '检索所有硫化机
        dtLHJ = objHWSetting.selectAllLHJ
        '清空画面现有硫化机
        TLP.Controls.Clear()
        Dim lhj(dtLHJ.Rows.Count) As ucLHJ
        Dim _localIP As String = GetIPAddr()
        '循环所有硫化机
        For i As Integer = 0 To dtLHJ.Rows.Count - 1
            '将硫化机组件显示到画面上

            Dim st As ucLHJ.LHJ_STATUS '= ucLHJ.LHJ_STATUS.LOSE_CONNECT
            If DBNULLtoBlank(dtLHJ(i)("loc_row")) <> "" Then
                Dim _row As String = dtLHJ(i)("loc_row")
                Dim _col As String = dtLHJ(i)("loc_col")

                If dtLHJ(i)("status") = "0" Then
                    st = ucLHJ.LHJ_STATUS.EMPTY      '空闲
                    lhj(i) = New ucLHJ(st, _row, _col, dtLHJ(i)("equip_id"), "", dtLHJ(i)("equip_id"))
                    lhj(i).Interval = 0
                ElseIf dtLHJ(i)("status") = "1" Then
                    st = ucLHJ.LHJ_STATUS.BUSY      '忙
                    lhj(i) = New ucLHJ(st, _row, _col, dtLHJ(i)("equip_id"), DBNULLtoBlank(dtLHJ(i)("prod_id")), dtLHJ(i)("equip_id"))
                    Dim _interval As Integer = 0
                    If Not IsDBNull(dtLHJ(i)("prod_spec_id")) Then
                        _interval = objHWSetting.getSpecIntervalbySpecID(dtLHJ(i)("prod_spec_id"))
                    End If
                    lhj(i).Interval = _interval
                ElseIf dtLHJ(i)("status") = "2" Then
                    st = ucLHJ.LHJ_STATUS.LOSE_CONNECT  '失联
                    lhj(i) = New ucLHJ(st, _row, _col, dtLHJ(i)("equip_id"), "", dtLHJ(i)("equip_id"))
                    lhj(i).Interval = 0 '30 * 1000 '半分钟
                ElseIf dtLHJ(i)("status") = "3" Then
                    st = ucLHJ.LHJ_STATUS.WAIT_OUT      '要出炉
                    lhj(i) = New ucLHJ(st, _row, _col, dtLHJ(i)("equip_id"), DBNULLtoBlank(dtLHJ(i)("prod_id")), dtLHJ(i)("equip_id"))
                    Dim _interval As Integer = 0
                    If Not IsDBNull(dtLHJ(i)("prod_spec_id")) Then
                        _interval = objHWSetting.getSpecIntervalbySpecID(dtLHJ(i)("prod_spec_id"))
                    End If
                    lhj(i).Interval = _interval
                ElseIf dtLHJ(i)("status") = "4" Then
                    st = ucLHJ.LHJ_STATUS.ERR           '温度或压力Error
                    lhj(i) = New ucLHJ(st, _row, _col, dtLHJ(i)("equip_id"), DBNULLtoBlank(dtLHJ(i)("prod_id")), dtLHJ(i)("equip_id"))
                    Dim _interval As Integer = 0
                    If Not IsDBNull(dtLHJ(i)("prod_spec_id")) Then
                        _interval = objHWSetting.getSpecIntervalbySpecID(dtLHJ(i)("prod_spec_id"))
                    End If
                    lhj(i).Interval = _interval
                ElseIf dtLHJ(i)("status") = "5" Then
                    st = ucLHJ.LHJ_STATUS.REPAIRS      '维修
                    lhj(i) = New ucLHJ(st, _row, _col, dtLHJ(i)("equip_id"), "", dtLHJ(i)("equip_id"))
                    lhj(i).Interval = 0 '30 * 1000
                End If
                
                If IsNothing(lhj(i)) = False Then

                    lhj(i).ServerIp = _localIP
                    lhj(i).ServerPort = "2000"
                    lhj(i).LhjIp = dtLHJ(i)("ip")
                    lhj(i).LhjPort = CInt(dtLHJ(i)("port"))
                    lhj(i).Spec_ID = DBNULLtoBlank(dtLHJ(i)("prod_spec_id"))
                    'lhj(i)._ParentForm = Me

                    TLP.Controls.Add(lhj(i), _col - 1, _row - 1)

                    Me.Refresh()
                End If

            End If

        Next
    End Sub


    'Private Sub tmrRe_Tick(sender As Object, e As EventArgs) Handles tmrRe.Tick
    '    'For i As Integer = 0 To TLP.Controls.Count - 1
    '    '    Dim usr As ucLHJ = TLP.Controls(i)
    '    '    If usr.Status = ucLHJ.LHJ_STATUS.LOSE_CONNECT Or
    '    '        usr.Status = ucLHJ.LHJ_STATUS.EMPTY Then
    '    '        usr.lblLHJ_No.ForeColor = Color.Purple
    '    '        Me.Refresh()
    '    '        usr.check()
    '    '    End If
    '    'Next
    '    Me.Refresh()
    'End Sub
    Private Sub menuItemClose_Click(sender As Object, e As EventArgs) Handles menuItemClose.Click
        If MsgBox("如关闭系统，则停止监控硫化机！" & vbCrLf & "确定要关闭吗？", 4, Me.Text) = MsgBoxResult.Yes Then
            RemoveHandler Me.Closing, AddressOf frmLHJList_Closing
            Close()
        End If
    End Sub

    Private Sub frmLHJList_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        If MsgBox("如关闭系统，则停止监控硫化机！" & vbCrLf & "确定要关闭吗？", 4, Me.Text) = MsgBoxResult.Yes Then

        Else
            e.Cancel = True
        End If
    End Sub

    'Private Sub MenuItemSetting_Click(sender As Object, e As EventArgs)
    '    Dim frmSetting As New frmPLCadd
    '    frmSetting.ShowDialog()
    'End Sub
End Class